package net.sudot.chess.business.dao;

import net.sudot.chess.business.dto.CustomerDetailDTO;
import net.sudot.chess.business.model.Customer;
import org.springframework.stereotype.Repository;

import javax.persistence.TypedQuery;
import java.time.Instant;
import java.util.List;

/**
 * 客户信息
 *
 * @author tangjialin on 2018-03-03.
 */
@Repository
public class CustomerDao extends BaseDao<Customer> {

    /**
     * 获取房间玩家信息
     *
     * @param roomId 房间主键
     * @return 返回房间玩家信息
     */
    public List<CustomerDetailDTO> findListDetailByRoom(Long roomId) {
        String ql = "SELECT new net.sudot.chess.business.dto.CustomerDetailDTO(customer.id, customer.nickname, customer.head, customer.balance, roomCustomerRef.createdDate)" +
                " FROM Customer customer" +
                " INNER JOIN RoomCustomerRef roomCustomerRef ON roomCustomerRef.customerId = customer.id" +
                " WHERE roomCustomerRef.roomId = :roomId" +
                " ORDER BY roomCustomerRef.createdDate ASC";
        TypedQuery<CustomerDetailDTO> query = entityManager.createQuery(ql, CustomerDetailDTO.class);
        query.setParameter("roomId", roomId);
        return query.getResultList();
    }

    /**
     * 获取房间玩家信息
     *
     * @param roomId 房间主键
     * @return 返回房间玩家信息
     */
    public List<Customer> findListByRoom(Long roomId) {
        String ql = "SELECT customer" +
                " FROM Customer customer" +
                " INNER JOIN RoomCustomerRef roomCustomerRef ON roomCustomerRef.customerId = customer.id" +
                " WHERE roomCustomerRef.roomId = :roomId" +
                " ORDER BY roomCustomerRef.createdDate ASC";
        TypedQuery<Customer> query = entityManager.createQuery(ql, entityClass);
        query.setParameter("roomId", roomId);
        return query.getResultList();
    }

    /**
     * 获取客户信息
     *
     * @param mobile 客户手机号码
     * @return 返回客户信息
     */
    public Customer findByMobile(String mobile) {
        return find("mobile", mobile);
    }

    /**
     * 获取客户信息
     *
     * @param username 客户登录名
     * @return 返回客户信息
     */
    public Customer findByUsername(String username) {
        return find("username", username);
    }

    /**
     * 获取时间段内服务次数最少的GameMaster信息
     *
     * @param start 时间段起始时间(大于等于[>=]start)
     * @param end   时间段结束时间(小于[<]end)
     * @return 返回GameMaster信息
     */
    public Customer findMinCountByDate(Instant start, Instant end) {
        String ql = "SELECT customer" +
                " FROM Customer customer" +
                " LEFT JOIN RoomServiceRecord roomServiceRecord ON roomServiceRecord.gameMaster = customer.id" +
                "       AND roomServiceRecord.createdDate >= :start AND roomServiceRecord.createdDate < :end" +
                " WHERE customer.gameMaster = true AND customer.deleted = false AND customer.enabled = true AND customer.locked = false" +
                " GROUP BY customer.id" +
                " ORDER BY COUNT(roomServiceRecord.id) ASC";
        TypedQuery<Customer> query = entityManager.createQuery(ql, Customer.class);
        query.setParameter("start", start);
        query.setParameter("end", end);
        return uniqueResult(query);
    }

    /**
     * 获取房间管理员
     *
     * @param roomId 房间主键
     * @return 返回该房间管理员
     */
    public Customer findGameMasterByRoom(Long roomId) {
        String ql = "SELECT customer" +
                " FROM Customer customer" +
                " INNER JOIN RoomCustomerRef roomCustomerRef ON roomCustomerRef.customerId = customer.id" +
                " WHERE roomCustomerRef.roomId = :roomId AND customer.gameMaster = true";
        TypedQuery<Customer> query = entityManager.createQuery(ql, Customer.class);
        query.setParameter("roomId", roomId);
        return uniqueResult(query);
    }
}
